package com.quan.weixin.scheduler;

import javax.annotation.PostConstruct;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.quan.weixin.api.AccessToken;
import com.quan.weixin.api.AccessTokenApi;
import com.quan.weixin.common.WeixinContext;

/**
 * 定时获取AccessToken
 * @author yhaoquan
 *
 */
@Component
public class AccessTokenRefreshTask {

	@PostConstruct
	public void initMethod() {
		this.accessTokenRefreshJob();
	}
	
	/**
	 * 0/5 * * * * ? 	  每5秒执行一次
	 * 0 55 0/1 * * ?	 每个1小时55分获取一次
	 */
	@Scheduled(cron = "0 55 0/1 * * ?")
	public void accessTokenRefreshJob() {
		//获取AccessToken
		AccessToken accessToken = AccessTokenApi.getAccessToken();
		//保存AccessToken到上下文
		WeixinContext.setAccessToken(accessToken.getAccess_token());
	}
}
